<?php

namespace app\controller;
use think\facade\Db;
use app\library\LayuiJson;

class Push extends BaseController
{
    public function pushAdd(){
        $id = isset($_GET['id'])? $_GET['id'] : null;
        if (isset($id)){
            $push = Db::name('push')->where('id', $id)->find();
        }else{
            $push = ['id' => '', 'domain' => '', 'token' => ''];
        }
        return view("push_add", ['push' => $push]);
    }

    public function pushLinkAdd(){
        $id = input('id');
        $push = Db::name('push')->where('id',$id)->find();
        return view('add_link',['push' => $push]);
    }

    public function pushList(){
        return view("push_list");
    }

    public function pushSave(){
        $id = isset($_POST['id'])? $_POST['id'] : null;
        if (isset($id)){
            $push = Db::name('push')->where('id', $id)->find();
        }
        $push['domain'] = trim(input('domain'));
        $push['token'] = trim(input('token'));
        Db::name('push')->save($push);
        return 1;
    }

    public function pushLinkSave(){
        $domain_id = input('domain_id');
        $type = input('type');
        $str = trim(strtolower(str_replace("\r","",input('links'))));
        $arr = array_unique(explode("\n",$str));
        //注意,先去重！
        $pushs = Db::name('push_link')->field('link')->select()->toArray();
        $exist_obj_sites = [];
        foreach ($pushs as $row){
            $exist_obj_sites[] = $row['link'];
        }
        $data = [];
        $count = 0;
        foreach ($arr as $k=>$v){
            $link = trim($v);
            if (!in_array($link,$exist_obj_sites)){
                $data[$k]['link'] = $link;
                $data[$k]['type'] = $type;
                $data[$k]['domain_id'] = $domain_id;
            }else{
                $count++;
            }
        }
        Db::name('push_link')->insertAll($data);
        return $count;
    }

    public function pushLinkList(){
        $domain_id = input('id');
        $links = Db::name('push_link')->where('domain_id',$domain_id)->select()->toArray();
        foreach ($links as $v){
            echo $v['link'].'<br>';
        }
    }

    public function pushAjax(){
        $page = intval(input('page'));//当前页
        $limit = intval(input('limit'));//每页条数
        $search = str_replace(['http://','https://',"www.",'/'],'',strtolower(trim(input('search'))));//处理搜索内容
        $offset = ($page-1) * $limit;
        if (empty($search)){
            $count = Db::name('push')->count();
            $data = Db::name('push')->order('id','desc')->limit($offset, $limit)->select()->toArray();
        }else{
            $data = Db::name('push')->where('domain','like',"%$search%")->select()->toArray();
            $count = 0;//不进行分页
        }
        return LayuiJson::tableJson($data, $count);
    }

    public function pushDel(){
        //单个删除
        if (isset($_POST['id'])){
            $ids = $_POST['id'];
        }else{//批量删除
            $data = input('data');
            $ids = '';
            foreach ($data as $v){
                $ids .= $v['id'].',';
            }
        }
        Db::name('push')->where('id','in', rtrim($ids ,","))->delete();
        return 1;
    }

    public function pushLinkGet(){
        $domain_id = input('id');
        $push = Db::name('push')->where('id',$domain_id)->find();
        $main = $push['domain'];
        $date = date('y-m-d');
        $log = "../task/log/$date/$main.log";
        if (file_exists($log)){
            echo file_get_contents($log);
        }else{
            echo "今日无推送记录";
        }
    }

}